﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Cellula.Model.Content;
using Cn.Edwon.Web.Support;
using System.Reflection;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Client;
using Cn.Edwon.Web;
using Cn.Cellula.Biz;

namespace Cn.Cellula.Website.Admin.Content.Dept {
    public partial class Edit : PageSupport {
        String Key = null;
        String ParentKey = null;
        IDeptBiz biz = BizFactory.Get<IDeptBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            Key = Request.QueryString["id"];
            ParentKey = Request.QueryString["pid"];
            if (!Page.IsPostBack) {
                InitForm();
                if (!String.IsNullOrEmpty(Key)) {
                    FillForm(Key);
                } else {
                    if (!String.IsNullOrEmpty(ParentKey))
                        this.ddl_Parent.SelectedValue = ParentKey;
                }
            }
        }

        private void InitForm() {
            this.ddl_Parent.DataSource = biz.LoadDept2ListItem(this.OwnerKey, "..", Key, false);
            this.ddl_Parent.DataValueField = "Value";
            this.ddl_Parent.DataTextField = "Text";
            this.ddl_Parent.DataBind();
        }

        private void FillForm(String key) {
            TSysDeptEO eo = biz.Load<TSysDeptEO>(key, this.OwnerKey);
            if (eo != null) {
                this.txt_Alias.Text = eo.DeptAlias;
                this.txt_Name.Text = eo.DeptName;
                this.ddl_Parent.SelectedValue = eo.ParentKey;
                this.txt_DeptLevel.Text = (eo.DeptLevel==null)?"":eo.DeptLevel + "";
                this.txt_Url.Text = eo.Url;
                this.txt_Desc.Text = eo.Description;
            }
        }

        private void BindData2EO(ref TSysDeptEO eo) {
            eo.DeptAlias = this.txt_Alias.Text;
            eo.DeptName = this.txt_Name.Text;
            eo.ParentKey = this.ddl_Parent.SelectedValue;
            try {
                eo.OrderNumber = Convert.ToInt32(this.txt_OrderNumber.Text);
            } catch { }
            try {
                eo.DeptLevel = Convert.ToInt32(this.txt_DeptLevel.Text);
            } catch {
                eo.DeptLevel = 0;
            }
            if (eo.DeptLevel > BizConst.MAX_LEVEL) {
                SiteInfo.Send(Page, "", "部门等级值太高，最大不能超过" + BizConst.MAX_LEVEL);
                return;
            }
            eo.OrderNumber = 0;
            eo.Url = this.txt_Url.Text;
            eo.Description = this.txt_Desc.Text;
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            TSysDeptEO eo = new TSysDeptEO();
            if (String.IsNullOrEmpty(Key)) {
                BindData2EO(ref eo);
                TSysDeptEO _eo = biz.LoadByName(eo.DeptName, this.OwnerKey);
                if (_eo != null) {
                    SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.Dept.NameExist);
                    return;
                } else {
                    eo.OwnerKey = this.OwnerKey;
                    biz.Save<TSysDeptEO>(eo);
                    BizFactory.Get<ILogBiz>().LogInsert("部门", eo.DeptName, this.Actor, Request);
                }
            } else {//update
                eo = biz.Load<TSysDeptEO>(Key, this.OwnerKey);
                BindData2EO(ref eo);
                biz.Update<TSysDeptEO>(eo);
                BizFactory.Get<ILogBiz>().LogUpdate("部门", eo.DeptName, this.Actor, Request);
            }
            Response.Redirect("List.aspx?&t=" + Key);
            //SiteInfo.SendSucceed(Page, "List.aspx?tid="+TypeKey+"&t=" + Key);
        }
    }
}